package com.gopay.bis.service.bank.payconfig;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.beanutils.BeanUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.gopay.common.constants.proccode.ProcCodeConstants;
import com.gopay.common.constants.txncd.IntTxnCd;
import com.gopay.common.domain.bank.payconfig.PayConfigRequest;
import com.gopay.common.domain.bank.payconfig.PayConfigResult;
import com.gopay.common.domain.bank.payconfig.bo.PayConfigInfo;
import com.gopay.common.domain.bis.BaseBisBankConfig;
import com.gopay.common.exception.GopayException;
import com.gopay.remote.bank.PayConfigRemoteService;

/**
 * 
 * @author liujy
 * 
 */
@Service("payConfigService")
public class PayConfigServiceImpl implements PayConfigRemoteService {

	private final Logger logger = LoggerFactory
			.getLogger(PayConfigServiceImpl.class);

	@Resource(name = "payConfigCommonService")
	private PayConfigCommonService commonService;

	@Override
	public PayConfigResult payConfig(PayConfigRequest request) {

		logger.error("payConfig request: " + request);

		PayConfigResult result = new PayConfigResult();

		try {

			// 获取该交易支持的银行业务产品--一级标签BisBankBizSpTranConfig
			List<PayConfigInfo> configs = commonService
					.getPayConfigInfosByIntTxnCd(request.getIntTxnCd());
			if (null == configs || configs.isEmpty()) {
				logger.error("该交易未配置银行业务产品：" + request.getIntTxnCd());
				throw new GopayException(ProcCodeConstants.PROC_CODE_100E7013);
			}

			for (PayConfigInfo config : configs) {
				dealPayConfig(config, request.getIntTxnCd());
			}
			result.setConfigs(configs);
			result.setResultCode(ProcCodeConstants.PROC_CODE_100S1000);

		} catch (GopayException e) {
			logger.error("PayConfigService.payConfig", e);
			result.setResultCode(e.getErrCode());
		} catch (Exception e) {
			logger.error("PayConfigService.payConfig", e);
			result.setResultCode(ProcCodeConstants.PROC_CODE_100F1002);
		}

		logger.error("payConfig request: " + request + ", result: " + result);
		return result;
	}

	@Override
	public PayConfigResult payConfigForOnLine() {
		List<BaseBisBankConfig> intrconfigs = commonService
				.getAllBcdlIntrConfig();
		return payConfigForOffLineOrOnLine(intrconfigs);
	}

	@Override
	public PayConfigResult payConfigForOffLine() {
		List<BaseBisBankConfig> intrconfigs = commonService
				.getAllEbankCustConfig();
		return payConfigForOffLineOrOnLine(intrconfigs);
	}

	@Override
	public boolean repeatLoadPayConfig() {
		commonService.repeatInitBankBizProdAndTranBankConfigs();
		commonService.repeatInitBizProdSpBankConfigs();
		commonService.repeatInitBizProdSpBankConfigs();
		return true;
	}
	
	@Override
	public List<String> getQuickConfig(String bankBizProd){
		// 查询银行业务产品支持的银行
		List<String> bankBizProdBankConfigs = commonService.getBankBizProdBankConfigsByBankBizProd(bankBizProd);
		return bankBizProdBankConfigs;
	}
	/**
	 * 线上、线下付款银行
	 * 
	 * @param intrconfigs
	 * @return
	 */
	private PayConfigResult payConfigForOffLineOrOnLine(
			List<BaseBisBankConfig> intrconfigs) {

		PayConfigResult result = new PayConfigResult();

		try {
			if (null != intrconfigs && !intrconfigs.isEmpty()) {

				List<PayConfigInfo> configs = new ArrayList<PayConfigInfo>();// 支付配置
				PayConfigInfo info = new PayConfigInfo();
				List<String> bankCodes = new ArrayList<String>();

				for (int i = 0, size = intrconfigs.size(); i < size; i++) {
					BaseBisBankConfig config = intrconfigs.get(i);
					bankCodes.add((String) BeanUtils.getSimpleProperty(config,
							"bankCode"));
				}

				info.setBankCodes(bankCodes);
				configs.add(info);
				result.setConfigs(configs);
			}

			result.setResultCode(ProcCodeConstants.PROC_CODE_100S1000);

		} catch (Exception e) {
			logger.error("PayConfigService.payConfigForOffLineOrOnLine", e);
			result.setResultCode(ProcCodeConstants.PROC_CODE_100F1002);
		}

		logger.error("payConfigForOffLineOrOnLine result: " + result);
		return result;
	}

	/**
	 * 
	 * @param config
	 * @param intTxnCd
	 */
	private void dealPayConfig(PayConfigInfo config, IntTxnCd intTxnCd) {
		// 付款渠道
		// TODO 此处确定不了付款渠道与银行的对应关系，所以暂不使用
		/*
		 * List<String> payChs = payConfigCommonService
		 * .getPayChConfigsByBankProd(config.getBankBizProd());
		 * config.setPayChannels(payChs);
		 */

		// 查询银行业务产品支持的银行
		List<String> bankBizProdBankConfigs = commonService
				.getBankBizProdBankConfigsByBankBizProd(config.getBankBizProd());
		if (null == bankBizProdBankConfigs || bankBizProdBankConfigs.isEmpty()) {
			return;
		}
		
		/** 暂不考虑BisBizProdSpTranBKConfig里的配置，直接使用银行业务产品支持的银行即可 */
		config.setBankCodes(bankBizProdBankConfigs);

		// 以下代码先注释，暂不使用
		
		// 查询银行业务产品+交易支持的银行
		/*List<String> bankBizProdAndTranBankConfigs = commonService
				.getBankBizProdAndTranBankConfigsByIntTxnCdAndProd(intTxnCd,
						config.getBankBizProd());
		if (null == bankBizProdAndTranBankConfigs
				|| bankBizProdAndTranBankConfigs.isEmpty()) {
			return;
		}*/

		/*List<String> bankCodes = new ArrayList<String>();
		for (String bankCode : bankBizProdAndTranBankConfigs) {
			if (bankBizProdBankConfigs.contains(bankCode)) {
				bankCodes.add(bankCode);
			}
		}
		config.setBankCodes(bankCodes);*/
	}
}
